package com.vitabuybuy.tv.ui.mine.setting.viewmodel

import android.view.View
import androidx.databinding.ObservableArrayList
import com.library.common.base.BaseViewModel
import com.vitabuybuy.tv.BR
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.dto.AgreementDto
import com.vitabuybuy.tv.inter.OnHelpCenterClickListener
import com.library.common.base.MessageModel
import com.library.common.ext.rxHttpRequest
import com.library.common.net.error.msg
import com.library.common.toast.XToast
import com.vitabuybuy.tv.network.SettingRepository
import me.tatarka.bindingcollectionadapter2.ItemBinding
import me.tatarka.bindingcollectionadapter2.OnItemBind

class HelpCenterViewModel : BaseViewModel() {

    private val itemOnClickListener = object : OnHelpCenterClickListener {
        override fun onItemClick(item: AgreementDto, position: Int) {
            defUI.msgEvent.postValue(MessageModel(1, arg1 = item.id))
        }
    }
    val list = ObservableArrayList<AgreementDto>()
    val listBinding: OnItemBind<AgreementDto> =
        OnItemBind { itemBinding: ItemBinding<Any>, position: Int, item: AgreementDto ->
            itemBinding.set(BR.itemBean, R.layout.item_help_center)
                .bindExtra(BR.position, position).bindExtra(BR.listenner, itemOnClickListener)
        }

    fun getData() {
        rxHttpRequest {
            onRequest = {
                list.addAll(SettingRepository.getHelpCenterList().await())
            }
            onError = {
                XToast.normal(it.msg)
            }
            isLoadingType = true
            isRefresh = true
        }
    }

    override fun onRetry() {
        super.onRetry()
        getData()
    }
}
